package com.mission.schedule.CommonDialog;

import android.app.Dialog;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.mission.schedule.R;
import com.mission.schedule.activity.CalenderNoteEditActivity;
import com.mission.schedule.applcation.App;
import com.mission.schedule.clock.QueryAlarmData;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.entity.ScheduleTable;
import com.mission.schedule.utils.DateUtil;
import com.mission.schedule.utils.SharedPrefUtil;

import java.util.Date;
import java.util.Map;

/**
 * 日程内容编辑对话框
 * Created by liwei on 2017/4/18.
 */

public class CalenderAddDContentDialog extends Dialog {


    CallBack callBack;

    SharedPrefUtil sharedPrefUtil = null;
    Context context;
    WindowManager windowManager;
    EditText addeverytask_tv;
    InputMethodManager imm;
    View view;


    public CalenderAddDContentDialog(@NonNull final Context context, @StyleRes int themeResId, WindowManager windowManager) {
        super(context, themeResId);
        this.context = context;
        this.windowManager = windowManager;

        setCanceledOnTouchOutside(true);
        sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);
        view = LayoutInflater.from(context).inflate(R.layout.dialog_add_d, null);
        setContentView(view);

        imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);

        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

        addeverytask_tv = view.findViewById(R.id.schcontent_et);


        findViewById(R.id.btn_ok).setOnClickListener(v -> save());
        imm.showSoftInput(addeverytask_tv, 0);
        findViewById(R.id.full).setOnClickListener(v -> {
            callBack.full(addeverytask_tv.getText().toString());
            dismiss();
        });

    }

    void save() {
        String content = addeverytask_tv.getText().toString();
        if (!content.isEmpty()) {
            try {
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
                sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.SCHTYPE, "1");
                App.getDBcApplication().insertScheduleData(content, DateUtil.formatDate(new Date()),
                        DateUtil.formatDateTimeHm(new Date()), 0,
                        0, 0, 1, 0,
                        0, 0, DateUtil.formatDateTime(new Date()), "", 0,
                        "", "", 0, "", DateUtil.formatDateTime(new Date()),
                        1, 0, 0, "完成任务", "g_88", "", 0, 0, 0, "",
                        "", 0, 0, 0, 0, "0", "0", "0");
                QueryAlarmData.writeAlarm(context.getApplicationContext());
                Intent intentAppWidget = new Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
                context.sendBroadcast(intentAppWidget);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }

            callBack.save();
            dismiss();
        } else {
            Toast.makeText(context, "内容不能为空", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        /* 触摸外部弹窗 */
//        if (isOutOfBounds(getContext(), event)) {
//            save();
//            return false;
//        }
        return super.onTouchEvent(event);
    }

    private boolean isOutOfBounds(Context context, MotionEvent event) {
        final int x = (int) event.getX();
        final int y = (int) event.getY();
        final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
        final View decorView = getWindow().getDecorView();
        return (x < -slop) || (y < -slop) || (x > (decorView.getWidth() + slop))
                || (y > (decorView.getHeight() + slop));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        window.setGravity(Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL);
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
    }

    public interface CallBack {
        void save();

        void full(String content);
    }

    public CallBack getCallBack() {
        return callBack;
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }
}
